package com.dyoud.api.shop.controller;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dyoud.api.ReturnInfo;
import com.dyoud.api.shop.employee.dto.AgreementDto;
import com.dyoud.api.shop.service.AgreementApiService;
import com.dyoud.core.ResponseBean;

/***
 * 
 * @author wangshuaifei
 * @date 2018/03/03
 *
 */
@RestController
@RequestMapping("agreement")
public class AgreementController {

    @Resource
    private AgreementApiService agreementApiService;

    /**
     * 添加协议
     * 
     * @param agreement
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseBean addAgreement(@RequestBody @Validated AgreementDto dto) {
        boolean isOK = agreementApiService.addAgreement(dto);
        if (isOK) {
            return new ResponseBean().success();
        }
        return new ResponseBean().fail(ReturnInfo.ADD_AGREEMENT_CODE, ReturnInfo.ADD_AGREEMENT_MESSAGE);

    }

    /**
     * 修改协议
     * 
     * @param agreement
     * @return
     */
    @RequestMapping(value = "/{agreementKey}", method = RequestMethod.PUT)
    public ResponseBean updateAgreement(@PathVariable("agreementKey") String agreementKey, @PathVariable("agreementContent") String agreementContent) {
        if (StringUtils.isEmpty(agreementKey)) {
            return new ResponseBean().fail(ReturnInfo.AGREEMENT_ID_NULL_CODE, ReturnInfo.AGREEMENT_ID_NULL_MESSAGE);
        }
        AgreementDto newdto = agreementApiService.getAgreement(agreementKey);
        if (newdto.getAgreementId() == null) {
            return new ResponseBean().fail(ReturnInfo.GET_UPDATE_AGREEMENT_CODE, ReturnInfo.GET_UPDATE_AGREEMENT_MESSAGE);
        }
        newdto.setAgreementContent(agreementContent);
        boolean isOK = agreementApiService.updateAgreement(newdto);
        if (!isOK) {
            return new ResponseBean().fail(ReturnInfo.UPDATE_AGREEMENT_CODE, ReturnInfo.UPDATE_AGREEMENT_MESSAGE);
        }
        return new ResponseBean().success();
    }

    /**
     * 获取协议
     * 
     * @param agreementKey
     * @return
     */
    @RequestMapping(value = "/{agreementKey}", method = RequestMethod.GET)
    public ResponseBean getAgreement(@PathVariable("agreementKey") String agreementKey) {
        if (StringUtils.isEmpty(agreementKey)) {
            return new ResponseBean().fail(ReturnInfo.AGREEMENT_ID_NULL_CODE, ReturnInfo.AGREEMENT_ID_NULL_MESSAGE);
        }
        AgreementDto dto = agreementApiService.getAgreement(agreementKey);
        if (dto == null) {
            return new ResponseBean().fail(ReturnInfo.GET_UPDATE_AGREEMENT_CODE, ReturnInfo.GET_UPDATE_AGREEMENT_MESSAGE);
        }
        return new ResponseBean().success(dto);
    }

    /***
     * 分页查询协议
     * 
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseBean getAllAgreement(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return new ResponseBean().success(agreementApiService.getAllAgreement(pageNo, pageSize));
    }
}
